package org.threadCase6;

import java.util.ArrayList;
import java.util.Collections;

public class RunnableDemo extends Thread {
    public static ArrayList<Integer> prize=new ArrayList<>();
    public ArrayList<Integer> money=new ArrayList<>();
    public int totalMoney=0;
    public int maxMoney=0;
    static {
        Collections.addAll(prize,10,5,20,50,100,200,2,300,80,700);
    }
    @Override
    public void run() {
        while (true){
            synchronized (RunnableDemo.class){
                if (prize.isEmpty()){ // 使用 isEmpty() 检查列表是否为空
                    System.out.println(Thread.currentThread().getName()+" 抱歉，当前抽奖已经结束。");
                    break;
                } else {
                    Collections.shuffle(prize);
                    int award =prize.get(0);
                    totalMoney+=award;
                    if (award>maxMoney){
                        maxMoney=award;
                    }
                    money.add(award);
                    prize.remove(0);
                }
            }
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
      synchronized (prize){
          System.out.println("在此次抽奖过程中，"+Thread.currentThread().getName()+"共产生了"+money.size()+"个奖项");
          System.out.print("分别为：");
          for (int i = 0; i < money.size(); i++) {
              if (i==money.size()-1){
                  System.out.print(money.get(i));
              }else {
                  System.out.print(money.get(i)+",");
              }
          }
          System.out.println("最高奖项为："+maxMoney+"总计金额为"+totalMoney+"元");
      }
    }
}

